package com.ym.domain.common.util;


import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class DateComUtil {

    public static final String DATE_FORMAT_YYYY_MM_DD_HH_MM_SS_SSS = "yyyy-MM-dd HH:mm:ss SSS";
    public static final String DATE_FORMAT_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_FORMAT_YYYY_MM_DD = "yyyy-MM-dd";
    public static final String DATE_FORMAT_YYYY_MM = "yyyy-MM";
    public static final String DATE_FORMAT_YYYY = "yyyy";
    public static final String DATE_FORMAT_HH_MM_SS = "HH:mm:ss";
    public static final String DATE_FORMAT_MM_DD = "MM-dd";
    public static final String DATE_FORMAT_MM_DD_HH_MM_SS = "MM-dd HH:mm:ss";
    public static final String DATE_FORMAT_MM_DD_HH_MM = "MM-dd HH:mm";


    public static Date getDateNow() {
        return new Date();
    }

    public static String getLocalDateTimeNowYyyyMmDdHhMmSsStr() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_FORMAT_YYYY_MM_DD_HH_MM_SS);
        return getLocalDateTimeNow().format(formatter);
    }

    public static LocalDate getLocalDateNow() {
        return LocalDate.now();
    }

    public static LocalDateTime getLocalDateTimeNow() {
        return LocalDateTime.now();
    }

    public static Long getLocalDateTimeNowTimeStamp() {
        return System.currentTimeMillis();
    }

    public static String getDateFormatStr(Date date, String format) {
        return new SimpleDateFormat(format).format(date);
    }

    public static String getLocalDateFormatStr(LocalDate localDate, String format) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        return localDate.format(formatter);
    }

    public static Long getInputLocalDateToTimeStamp(LocalDate localDate) {
        long rtnTimeStamp = 0L;
        if (null != localDate) {
            LocalDateTime localDateTime = localDate.atStartOfDay();
            Timestamp timestamp = Timestamp.valueOf(localDateTime);
            rtnTimeStamp = timestamp.getTime();
        }
        return rtnTimeStamp;
    }

    public static Long getInputLocalDateStrToTimeStamp(String inputDateStr) {
        long rtnTimeStamp = 0L;
        if (StringComUtil.isNotEmpty(inputDateStr)) {
            LocalDate localDate = LocalDate.parse(inputDateStr, DateTimeFormatter.ofPattern(DATE_FORMAT_YYYY_MM_DD));
            LocalDateTime localDateTime = localDate.atStartOfDay();
            Timestamp timestamp = Timestamp.valueOf(localDateTime);
            rtnTimeStamp = timestamp.getTime();
        }
        return rtnTimeStamp;
    }

    public static LocalDate getLocalDateByStr(String inputDateStr) {
        LocalDate localDate = null;
        if (StringComUtil.isNotEmpty(inputDateStr)) {
            localDate = LocalDate.parse(inputDateStr, DateTimeFormatter.ofPattern(DATE_FORMAT_YYYY_MM_DD));
        }
        return localDate;
    }

    public static String getLocalDateNowYyyyMmDd() {
        LocalDate localDateNow = getLocalDateNow();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_FORMAT_YYYY_MM_DD);
        return localDateNow.format(formatter);
    }

    public static String getLocalDateBeforeNowYyyyMmDd(long beforeDay) {
        LocalDate localDateBeforeNow = getLocalDateNow().minusDays(beforeDay);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_FORMAT_YYYY_MM_DD);
        return localDateBeforeNow.format(formatter);
    }

    public static Long getDiffSecondBetweenDate(LocalDateTime littleTime, LocalDateTime biggerTime) {
        Duration duration = Duration.between(littleTime, biggerTime);
        return duration.getSeconds();
    }

}
